//工器具表中最大id
var maxApplianceId = 0
//最大用户Id
var maxUserId = 0
//仓库最大id
var maxWarehouseId = 0
//借用历史最大id
var maxBorrowId = 0
//当前登录用户
var currentUser = {};

//初始化数据库、创建表
function initDB() {
	currentDb();
	createTables();
	
	if (maxApplianceId == 0) {
		setTimeout(getApplianceMaxId(), 500)
	}
	if (maxUserId == 0) {
		setTimeout(getUserMaxId(), 500)
	}
	if (maxWarehouseId == 0) {
		setTimeout(getWarehouseMaxId(), 500)
	}
	if (maxBorrowId == 0) {
		setTimeout(getBorrowMaxId(), 500)
	}
}

//获取数据库，不存在就创建
function currentDb() {
	var db = openDatabase("applianceDb", "1.0", "工器具管理数据库", 1024 * 1024);
	return db;
}

/*
创建表
注意：appliance_table中的status有三个值1、2、3表示在库内、在库外、待报废
	 user_table中的role有三个值：1、2、3代表系统管理员、库管理员、带电班成员
*/
function createTables() {
	var db = currentDb();
	db.transaction(function (tx) {  
		//创建所有工器具表
	   tx.executeSql('CREATE TABLE IF NOT EXISTS appliance_table (id unique, category, name, addTime, status, warehouse, warehouseId)');
	   //用户列表
	   tx.executeSql('CREATE TABLE IF NOT EXISTS user_table (id unique, name, role, warehouse, warehouseId)');
		//仓库列表
		tx.executeSql('CREATE TABLE IF NOT EXISTS warehouse_table (id unique, name, address, area)');
		//领用记录列表
		tx.executeSql('CREATE TABLE IF NOT EXISTS borrow_table (id unique, warehouse, warehouseId, applianceId, category, applianceName, applianceStatus, borrowTime, borrowUserName, borrowUserId)');
	});
}

//获取工器具列表
function getAppliancList(callBack) {
	var db = currentDb();
	db.transaction(function (tx) {
	   tx.executeSql('SELECT * FROM appliance_table', [], function (tx, results) {
		  callBack(results.rows)
	   }, null);
	});
}

//根据status筛选
function getAppliancListFilterStatus(status, callBack) {
	var sql = 'SELECT * FROM appliance_table WHERE status=' + status;
	var db = currentDb();
	db.transaction(function (tx) {
	   tx.executeSql(sql, [], function (tx, results) {
		  callBack(results.rows)
	   }, null);
	});
}

//根据名称、类别、 status筛选
function getAppliancListFilterCategory(category, name, status, callBack) {
	var sql = 'SELECT * FROM appliance_table WHERE ';
	sql = sql + 'category LIKE "%' + category + '%"';
	sql = sql + ' and name LIKE "%' + name + '%"';
	// if (category.length > 0) {
		
	// }
	// if (name.length > 0) {
		
	// }
	if (parseInt(status) > 0) {
		sql = sql + ' and status=' + status;
	}
	
	console.log(sql);
	var db = currentDb();
	db.transaction(function (tx) {
	   tx.executeSql(sql, [], function (tx, results) {
		  callBack(results.rows)
	   }, null);
	});
}

//添加工器具
function addAppliance(category, name, addTime, status, warehouse, warehouseId, callback) {
	maxApplianceId = localStorage.getItem("maxApplianceId");
	maxApplianceId = parseInt(maxApplianceId) + 1
	localStorage.setItem("maxApplianceId", maxApplianceId);
	console.log(maxApplianceId)
	var db = currentDb();
	db.transaction(function (tx) {
	   tx.executeSql('INSERT INTO appliance_table (id, category, name, addTime, status, warehouse, warehouseId) VALUES (?, ?, ?, ?, ?, ?, ?)', [maxApplianceId, category, name, addTime, status, warehouse, warehouseId], function(){
		   callback(true);
	   });
	});
}

//删除工器具数据
function deleteAppliance(id) {
	var db = currentDb();
	db.transaction(function (tx) {
	    tx.executeSql('DELETE FROM appliance_table WHERE id=?', [id]);
	});
}

//更新工器具数据
function updateAppliance(id, status, callback) {
	var db = currentDb();
	db.transaction(function (tx) {
		tx.executeSql('UPDATE appliance_table SET status=? WHERE id=?', [status, id], function(){
			callback(true);
		});
	});
}

/*
添加用户
role有三个值：1、2、3代表系统管理员、库管理员、带电班成员
*/
function addUser(name, role, warehouse, warehouseId, callback) {
	maxUserId = localStorage.getItem("maxUserId");
	maxUserId = parseInt(maxUserId) + 1
	localStorage.setItem("maxUserId", maxUserId);
	console.log(maxUserId)
	var db = currentDb();
	db.transaction(function (tx) {
	   tx.executeSql('INSERT INTO user_table (id, name, role, warehouse, warehouseId) VALUES (?, ?, ?, ?, ?)', [maxUserId, name, role, warehouse, warehouseId], function(){
		   callback(true);
	   });
	});
}

//删除用户
function deleteUser(id) {
	var db = currentDb();
	db.transaction(function (tx) {
	    tx.executeSql('DELETE FROM user_table WHERE id=?', [id]);
	});
}

//获取用户列表
function getUserList(callBack) {
	var db = currentDb();
	db.transaction(function (tx) {
	   tx.executeSql('SELECT * FROM user_table', [], function (tx, results) {
		  callBack(results.rows)
	   }, null);
	});
}

//获取仓库列表
function getWarehouseList(callBack) {
	var db = currentDb();
	db.transaction(function (tx) {
	   tx.executeSql('SELECT * FROM warehouse_table', [], function (tx, results) {
		  callBack(results.rows)
	   }, null);
	});
}

//添加仓库
function addWarehouse(name, address, area, callback) {
	maxWarehouseId = localStorage.getItem("maxWarehouseId");
	maxWarehouseId = parseInt(maxWarehouseId) + 1
	localStorage.setItem("maxWarehouseId", maxWarehouseId);
	console.log(maxWarehouseId)
	var db = currentDb();
	db.transaction(function (tx) {
	   tx.executeSql('INSERT INTO warehouse_table (id, name, address, area) VALUES (?, ?, ?, ?)', [maxWarehouseId, name, address, area], function(){
		   callback(true);
	   });
	});
}

//删除仓库
function deleteWarehouse(id) {
	var db = currentDb();
	db.transaction(function (tx) {
	    tx.executeSql('DELETE FROM warehouse_table WHERE id=?', [id]);
	});
}

//获取领用记录列表
function getBorrowList(callBack) {
	var db = currentDb();
	db.transaction(function (tx) {
	   tx.executeSql('SELECT * FROM borrow_table', [], function (tx, results) {
		  callBack(results.rows)
	   }, null);
	});
}

//添加领用记录
function addBorrow(warehouse, warehouseId, applianceId, category, applianceName, applianceStatus, borrowTime, borrowUserName, borrowUserId) {
	maxBorrowId = localStorage.getItem("maxBorrowId");
	maxBorrowId = parseInt(maxBorrowId) + 1
	localStorage.setItem("maxBorrowId", maxBorrowId);
	console.log(maxBorrowId)
	var db = currentDb();
	db.transaction(function (tx) {
	   tx.executeSql('INSERT INTO borrow_table (id, warehouse, warehouseId, applianceId, category, applianceName, applianceStatus, borrowTime, borrowUserName, borrowUserId) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)', [maxBorrowId, warehouse, warehouseId, applianceId, category, applianceName, applianceStatus, borrowTime, borrowUserName, borrowUserId]);
	});
}

//更新领用记录数据
function updateBorrow(id, status, callback) {
	var db = currentDb();
	db.transaction(function (tx) {
		tx.executeSql('UPDATE borrow_table SET applianceStatus=? WHERE id=?', [status, id], function(){
			callback(true);
		});
	});
}

//获取工器具列表最大id
function getApplianceMaxId() {
	var db = currentDb();
	db.transaction(function (tx) {
	   tx.executeSql('SELECT * FROM appliance_table', [], function (tx, results) {
	      var id = 0
		  if (results.rows.length > 0) {
			var lastIndex = results.rows.length - 1
			id = results.rows.item(lastIndex).id;
		  }
	      maxApplianceId = id 
		  localStorage.setItem("maxApplianceId", maxApplianceId);
	   }, null);
	});
}

//获取用户列表最大id
function getUserMaxId() {
	var db = currentDb();
	db.transaction(function (tx) {
	   tx.executeSql('SELECT * FROM user_table', [], function (tx, results) {
	      var id = 0
		  if (results.rows.length > 0) {
			var lastIndex = results.rows.length - 1
			id = results.rows.item(lastIndex).id;
		  }
	      maxUserId = id
		  localStorage.setItem("maxUserId", maxUserId);
	   }, null);
	});
}

//获取仓库列表最大id
function getWarehouseMaxId() {
	var db = currentDb();
	db.transaction(function (tx) {
	   tx.executeSql('SELECT * FROM warehouse_table', [], function (tx, results) {
	      var id = 0
		  if (results.rows.length > 0) {
			var lastIndex = results.rows.length - 1
			id = results.rows.item(lastIndex).id;
		  }
	      maxWarehouseId = id
		  localStorage.setItem("maxWarehouseId", maxWarehouseId);
	   }, null);
	});
}

//获取领用列表最大id
function getBorrowMaxId() {
	var db = currentDb();
	db.transaction(function (tx) {
	   tx.executeSql('SELECT * FROM borrow_table', [], function (tx, results) {
	      var id = 0
		  if (results.rows.length > 0) {
			var lastIndex = results.rows.length - 1
			id = results.rows.item(lastIndex).id;
		  }
	      maxBorrowId = id
		  localStorage.setItem("maxBorrowId", maxBorrowId);
	   }, null);
	});
}

